<?php
use yii\helpers\Url;
?>
<div class="layui-fluid">
    <div class="layui-card">

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin" data-type="batchdel">删除</button>
            </div>

            <table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>

            <script type="text/html" id="create_time">
                {{layui.util.toDateString(d.login_time*1000)}}
            </script>
            <script type="text/html" id="buttonTpl">
                <input type="checkbox" {{ d.status == 1 ? 'checked':'' }} name="status" lay-skin="switch" value="{{d.status}}" lay-filter="UserStatus" switchId={{d.id}} {{d.id==1?'disabled':''}} lay-text="启用|禁用">

            </script>
            <script type="text/html" id="table-useradmin-admin">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<?php $this->beginBlock('footer')?>
<script>
    layui.config({
        base: '<?=Url::base()?>/resources/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table'], function(){
        var $ = layui.$
            ,form = layui.form
            ,table = layui.table;
//管理员管理
        table.render({
            elem: '#LAY-user-back-manage'
            ,url: '<?=Url::toRoute([$this->context->id.'/index'])?>' //模拟接口
            ,limit: '<?=$pageSize?>' //每页默认显示的数量
            ,method:'post'
            ,where:{'_csrf-backend':'<?=Yii::$app->request->csrfToken?>'}
            ,page: true
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', width: 80, title: 'ID', sort: true}
                ,{field: 'uid', title: '用户名',templet:'<div>{{d.user.name}}</div>'}
                ,{field: 'ip', title: '登录IP'}
                ,{field: 'address', title: '登录地址'}
                ,{field: 'login_time', title: '登录时间',templet: '#create_time', sort: true}
                ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
            ]]
            // ,toolbar:true
            ,text: '对不起，加载出现异常！'
            ,done:function (res) {
                //console.log(res);
            }
        });

        //事件
        var active = {
            batchdel: function(){
                var checkStatus = table.checkStatus('LAY-user-back-manage')
                    ,checkData = checkStatus.data; //得到选中的数据

                if(checkData.length === 0){
                    return layer.msg('请选择数据');
                }

                layer.prompt({
                    formType: 1
                    ,title: '敏感操作，请验证口令'
                }, function(value, index){
                    layer.close(index);

                    layer.confirm('确定删除吗？', function(index) {

                        //执行 Ajax 后重载
                        /*
                        admin.req({
                          url: 'xxx'
                          //,……
                        });
                        */
                        table.reload('LAY-user-back-manage');
                        layer.msg('已删除');
                    });
                });
            }
        };
        table.on('tool(LAY-user-back-manage)',function (obj) {
            let id = obj.data.id;
            if(obj.event='del'){
                layer.confirm('你确定要删除该数据？',function (index) {
                    $.get('<?=Url::toRoute([$this->context->id.'/del'])?>',{id:id},function (res) {
                        if(res.status==200){
                            layer.msg(res.msg,{icon:6,time:1000},function () {
                                console.log(obj);
                                obj.del();
                                layer.close(index);
                            })
                        }else {
                            layer.msg(res.msg,{icon:5,time:1000})
                        }
                    },'json')
                })
            }
        });
        $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

<?php $this->endBlock()?>


